<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="win-frame">
<head>
<title>代码表管理-编辑代码表</title>
<jsp:include page="/common/head.jsp"></jsp:include>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>代码表管理</span>
            <span class="split"></span>
            <span>编辑代码表</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

<form id="editForm" method="post">
     <div class="form-wrapper">
		<div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>名称</p>
	        <input type="text" id="name" name="name" value="${code.name}" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/>
			<input type="hidden" id="id" name="id" value="${code.id}" class="summer-input" />	
	     </div>
	     
		<div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>代码</p>
	        <input type="text" id="code" name="code" value="${code.code}"  class="summer-input"/>	
	     </div>
	     
		<div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>类型</p>
			<input type="text" id="type" name="type" class="summer-input summer-validatebox" data-validatebox="validType:['required']">	
	     </div>
  </div>
</form>

<div class="form-btn-wrapper" id="funcbar">
     <a class="btn btn-normal" id="closeBtn">关闭</a>
</div>
<script>
	var CODE_CONST_URL = {
        EDIT: basePath+ '/sys/code/edit',
        CHECK_EXIST: basePath + "/sys/code/checkCodeExist",
        INIT_MAIN: basePath + "/sys/code/init"
    };
	
	var permission = CommonUtil.getPermission('code');

$(function () {
	//初始化funcbar
	$('#funcbar').prepend($.funcbar({
        type: 'button',
        data: permission,
        permit: ['code:edit']
    }));
	
	//加载下拉列表
    initCombobox();
	
	//表单提交事件
	$('#funcbar').on('click', 'a[data-permit="code:edit"]', save);
    
    //关闭按钮事件
    $('#closeBtn, #goBack').on('click', backMain); 
    
    //校验代码code是否可用 
	validateExist('#code','code', CODE_CONST_URL.CHECK_EXIST, ['required','maxLength[50]','regexp'], "代码可用");
});
//代码表类型
function initCombobox(){
    var data = [
                    {
                        "code": 0,
                        "name":"非级联"
                    },
                    {
                        "code": 1,
                        "name":"级联"
                    }
            
                ];
     $('#type').combobox({
                     valueField: 'code',
                     textField: 'name',
                     data: data,
                     async:false,
                     defaultValue: '${code.type}'
    });
}
//保存
function save() {
    $("#editForm").form({
    	buttons : ['#saveBtn'],
    	ajax: {
            url : CODE_CONST_URL.EDIT
        },
        onSubmitSuccess: function (data) {
        	 $.stip({
	                type:'ok',//显示类型 有info、 ok、 danger
	                text: '保存成功！',
	                onBeforeHide: function () {
                	    backMain();
	               }
            });
        }
        
    }).form("submit");
}
//关闭
function backMain() {
	window.location.href = CODE_CONST_URL.INIT_MAIN;
}
//校验代码code是否可用 
function validateExist(id, name, url, validType, message) {
    $(id).validatebox({
        rules: {
            regexp: {
                validator: function (value,param) {
                    var result = false,
                        params = {}, 
                        key = name;
                        params[key] = value;
                        params['id'] = $('#id').val();
                    $.ajax({
                        type: 'post',
                        url: url,
                        data: params,
                        dataType: 'json',
                        async: false,
                        success: function (data) {
                            if (data.result == 'exist') {
                                result = false;
                            } else {
                                result = true;
                            }
                        }
                    });
                    return result;
                },
                message: message,
            }
        },
        validType: validType
    });
}


</script>
</body>
</html>